package com.lianzhihui.romeex.database;

import android.content.Context;

import com.lianzhihui.romeex.database.utils.OcMath;
import com.lianzhihui.romeex.utils.LogUtil;

import java.util.HashSet;
import java.util.Set;

import io.realm.DynamicRealm;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmMigration;
import io.realm.RealmSchema;
//import party.loveit.bip44forjava.core.ECKey;
//import party.loveit.bip44forjava.core.Sha256Hash;

/**
 * Copyright (C), 2019-2020
 * FileName: DataBaseConfig
 * Author: ZhaoFeng
 * Date: 2020/10/22 17:15
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
public class DataBaseConfig {
    private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
    static RealmConfiguration realmConfig = null;
    protected static Set<RealmConfiguration> mConfigsList = new HashSet<RealmConfiguration>();
    private static int version = 1;  // 数据库版本号
    public static int getDBVersion() {
        return version;
    }
    public static void setDBVersion(int version) {
        DataBaseConfig.version = version;
    }
    public static void initRealm(Context context) {
        //此处根据业务调用初始化，避免出现默认库bug
        LogUtil.debug("DataBaseConfig:","initRealm");
        if (Realm.getDefaultConfiguration() == null) {
            Realm.init(context);
        }
        if (!Realm.getDefaultConfiguration().getRealmFileName().equals("wallet")) {
            setWalletOCRealm();
        }
    }

    private static void setWalletOCRealm() {
//        ECKey ecKey = ECKey.fromPrivate(OcMath.toBigInt("0abc4301"));
//        byte[] sha256 = Sha256Hash.hash(ecKey.getPubKey());
//        LogUtil.debug("DataBaseConfig:","sha256---------"+OcMath.toHexStringNoPrefix(OcMath.toHexStringNoPrefix(sha256).getBytes()));
        realmConfig = new RealmConfiguration.Builder()
//                .schemaVersion(ConfigRealm.DB_VER)
                .migration(migration_wallet)
//       设置数据库密码
//                .encryptionKey(OcMath.toHexStringNoPrefix(sha256).getBytes())
                .encryptionKey("1234567890123456789012345678901234567890123456789012345678900000".getBytes())
                .name("wallet.realm")
                .schemaVersion(getDBVersion())
                .build();
        mConfigsList.add(realmConfig);
        Realm.setDefaultConfiguration(realmConfig);
    }

    public static Realm getRealm() {
        return Realm.getInstance(getRealmConfiguration());
    }

    public static RealmConfiguration getRealmConfiguration() {
        if (realmConfig == null) {
            setWalletOCRealm();
        }
        return realmConfig;
    }

    /**
     * 升级wallet_oc数据库
     */
    protected static RealmMigration migration_wallet = new RealmMigration() {//升级数据库
        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            RealmSchema schema = realm.getSchema();
            //数据库升级
            if (oldVersion == 1) {
//                schema.get("Rm_BaseTransRecord")
//                        .addField("isRead", boolean.class);
//                schema.get("Rm_BaseNotificationMsg")
//                        .addField("isRead", boolean.class);
                oldVersion++;
            }
        }
    };
}
